﻿using UnityEngine;
using System.Collections;
using Dict.DataModel;
using ProtoBuf.Message;

namespace Room
{
    public class RCUIServerMonster : RCUIBase
    {
        public RCUIServerMonster(RoomCharactor owner)
            : base(owner)
        {
            this.owner = owner;
            type = UIType.ServerMonster;
        }

        SideMonster monster;
        float timer = 1f;
        float curTime = 0f;
        float dis = 1.3f;

        public void Init(SideMonster monster)
        {
            this.monster = monster;
        }



        public override void Tick()
        {
            base.Tick();
            curTime += Time.deltaTime;
            if(curTime >= timer)
            {
                curTime = 0f;

                var self = owner.charactorManager.GetSelfCharactor();
                if (self != null)
                {
                    var selfAI = self.ai as RCAISelf;
                    if (self != null && !selfAI.AutoPathFinding)
                    {
                        var _dis = Vector3.Distance(owner.transform.position, self.transform.position);
                        if (_dis < dis)
                        {
                            OnClick();
                            curTime = 0f;
                        }
                    }
                }
            }
        }

        protected override void OnClick()
        {
            var ai = owner.ai as RCAIServerMonster;
            var self = owner.charactorManager.GetSelfCharactor();
            if(self!=null)
            {
                var ai0 = self.ai as RCAIActor;
                if(ai0!=null)
                {
                    if (ai.curState == ServerMonsterAIState.Normal 
                        && ai0.curState == PlayerAIState.Normal )
                    {
                        var state = ai0.GetCurrState() as PlayerAIStateNormal;
                        if (state.InvincibleTimer <= 0f)
                        {
                            var room = RoomManager.currentRoom as RoomField;
                            if (room != null)
                            {
                                StaticData.currClickMonster = monster;
                                var netManager = room.netManager as RoomNetField;
                                netManager.SendPve(monster.uuid);
                            }
                        }
                    }
                }
            }
        }
    }
}
